package com.xhd.db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.exception.DatabaseException;
import com.xhd.server.ServerConfig;

public class DBConn {
	 private static int jm254 = 0;
	  private DBConfig jm53;
	  private DBPool jm255;
	  private Connection jm256 = null;
	  private String jm257 = null;
	  private boolean jm258 = false;
	  private int jm259 = 0;
	  private boolean jm260 = true;
	  private boolean jm261 = false;
	  private boolean jm262 = false;
	  private boolean jm220 = false;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	    
	      try
	      {
	        DBConn.jm10 = Class.forName("com.sinocc.systemdbmanager.DBConn");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  protected DBConn(DBPool paramDBPool, Connection paramConnection, DBConfig paramDBConfig)
	    throws DatabaseException
	  {
	    this.jm255 = paramDBPool;
	    this.jm256 = paramConnection;
	    this.jm262 = false;
	    this.jm53 = paramDBConfig;
	  }

	  protected boolean reInit()
	  {
	    this.jm258 = false;
	    this.jm259 = 0;
	    this.jm220 = false;
	    return false;
	  }

	  protected boolean isBad()
	  {
	    return this.jm261;
	  }

	  protected void release()
	  {
	    if (this.jm256 == null)
	      return;
	    try
	    {
	      this.jm256.close();
	    }
	    catch (Exception localException)
	    {
	    }
	    finally
	    {
	      this.jm256 = null;
	    }
	  }

	  private synchronized String jm250()
	  {
	    if (jm254 <= -99999)
	      jm254 = -1;
	    else
	      jm254 -= 1;
	    return String.valueOf(jm254);
	  }

	  protected String getId()
	  {
	    if ((this.jm257 == null) || (this.jm257.length() < 1))
	      this.jm257 = jm250();
	    return this.jm257 + "/" + this.jm53.getId();
	  }

	  protected int getDBType()
	  {
	    return this.jm53.getDBType();
	  }

	  protected void setCheckConn(boolean paramBoolean)
	  {
	    this.jm260 = paramBoolean;
	    this.jm262 = (!(this.jm260));
	  }

	  private boolean jm251()
	  {
	    return (this.jm259 != 1);
	  }

	  private void jm252()
	    throws DatabaseException
	  {
	    Statement localStatement = null;
	    int i = 0;
	    try
	    {
	      String str = this.jm53.getPIdSQL();
	      if ((str == null) || (str.length() < 1))
	        if (getDBType() == 2)
	          str = "SELECT @@SPID";
	        else if (getDBType() == 1)
	          str = "SELECT sid||','||serial# as pid FROM v$session WHERE audsid=USERENV('SESSIONID')";
	      localStatement = this.jm256.createStatement();
	      if ((str != null) && (str.length() > 0))
	      {
	        this.jm257 = null;
	        ResultSet localResultSet = localStatement.executeQuery(str);
	        if (localResultSet != null)
	        {
	          if (localResultSet.next())
	            this.jm257 = localResultSet.getString(1);
	          localResultSet.close();
	        }
	      }
	      i = 1;
	      str = this.jm53.getInitSQL();
	      if ((str != null) && (str.length() > 0))
	      {
	        int j = str.indexOf("{sid}");
	        if (j > 0)
	        {
	          StringBuffer localStringBuffer = new StringBuffer(str.length() + 8);
	          localStringBuffer.append(str.substring(0, j));
	          localStringBuffer.append(ServerConfig.ServerId);
	          localStringBuffer.append(str.substring(j + 5));
	          str = localStringBuffer.toString();
	        }
	        localStatement.execute(str);
	      }
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	    }
	    finally
	    {
	      try
	      {
	        if (localStatement != null)
	          localStatement.close();
	      }
	      catch (SQLException localSQLException)
	      {
	      }
	    }
	    if (i != 0)
	      return;
	    this.jm261 = true;
	    throw new DatabaseException(30076, (this.jm255 == null) ? this.jm53.getId() : this.jm255.getId());
	  }

	  private Connection jm253()
	    throws DatabaseException
	  {
	    if (this.jm258)
	      jm9.error("Use aleady free database connection.");
	    if (!(this.jm262))
	    {
	      this.jm262 = true;
	      jm252();
	    }
	    if (this.jm220)
	      return this.jm256;
	    try
	    {
	      this.jm220 = true;
	      boolean bool = jm251();
	      this.jm256.setAutoCommit(!(bool));
	      if (bool)
	        jm9.sql(getId(), "**********  begin transaction", 0L);
	      int i = this.jm53.getTranLevel(bool);
	      if ((i >= 0) && (i != this.jm256.getTransactionIsolation()))
	        this.jm256.setTransactionIsolation(i);
	    }
	    catch (SQLException localSQLException)
	    {
	      throw new DatabaseException(30021);
	    }
	    return this.jm256;
	  }

	  public void commit()
	    throws DatabaseException
	  {
	    if ((this.jm256 == null) || (!(jm251())))
	      return;
	    try
	    {
	      this.jm256.commit();
	    }
	    catch (Exception localException1)
	    {
	      jm9.error("commit", localException1);
	      throw new DatabaseException(30022);
	    }
	    try
	    {
	      jm9.sql(getId(), "********** commit end transaction", 0L);
	      this.jm259 = 1;
	      this.jm256.setAutoCommit(true);
	    }
	    catch (Exception localException2)
	    {
	      jm9.error(localException2);
	    }
	  }

	  public void commitContinue()
	    throws DatabaseException
	  {
	    if ((this.jm256 == null) || (!(jm251())))
	      return;
	    try
	    {
	      this.jm256.commit();
	    }
	    catch (Exception localException1)
	    {
	      jm9.error("commit", localException1);
	      throw new DatabaseException(30022);
	    }
	    try
	    {
	      jm9.sql(getId(), "********** commit and beign transaction", 0L);
	    }
	    catch (Exception localException2)
	    {
	      jm9.error(localException2);
	    }
	  }

	  public void rollback()
	  {
	    if ((this.jm256 == null) || (!(jm251())))
	      return;
	    try
	    {
	      this.jm256.rollback();
	    }
	    catch (Exception localException1)
	    {
	      jm9.error(localException1);
	    }
	    try
	    {
	      jm9.sql(getId(), "********** rollback end transaction", 0L);
	      this.jm259 = 1;
	      this.jm256.setAutoCommit(true);
	    }
	    catch (Exception localException2)
	    {
	      jm9.error(localException2);
	    }
	  }

	  public void setTransation(boolean paramBoolean)
	  {
	    this.jm259 = ((paramBoolean) ? 2 : 1);
	  }

	  protected Statement createStatement()
	    throws SQLException, DatabaseException
	  {
	    try
	    {
	      return jm253().createStatement();
	    }
	    catch (SQLException localSQLException)
	    {
	      this.jm261 = true;
	      throw localSQLException;
	    }
	  }

	  protected PreparedStatement prepareStatement(String paramString)
	    throws SQLException, DatabaseException
	  {
	    try
	    {
	      return jm253().prepareStatement(paramString);
	    }
	    catch (SQLException localSQLException)
	    {
	      this.jm261 = true;
	      throw localSQLException;
	    }
	  }

	  protected CallableStatement prepareCall(String paramString)
	    throws SQLException, DatabaseException
	  {
	    try
	    {
	      return jm253().prepareCall(paramString);
	    }
	    catch (SQLException localSQLException)
	    {
	      this.jm261 = true;
	      throw localSQLException;
	    }
	  }

	  protected int kill()
	  {
	    if (this.jm256 == null)
	      return 2;
	    if ((this.jm257 == null) || (this.jm257.equals("0")) || (this.jm255 == null) || (this.jm257.charAt(0) == '-'))
	      return 1;
	    jm9.trace("kill: " + getId());
	    DBConn localDBConn = null;
	    Statement localStatement = null;
	    try
	    {
	      String str = null;
	      localDBConn = this.jm255.getInnerConnection();
	      localStatement = localDBConn.createStatement();
	      if (getDBType() == 2)
	        str = "kill " + this.jm257;
	      else if (getDBType() == 1)
	        str = "ALTER SYSTEM KILL SESSION '" + this.jm257 + "'";
	      localStatement.execute(str);
	      return 2;
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	      return 3;
	    }
	    finally
	    {
	      if (localDBConn != null)
	      {
	        if (localStatement != null)
	          try
	          {
	            localStatement.close();
	          }
	          catch (Exception localException)
	          {
	            jm9.error(localException);
	          }
	        localDBConn.close();
	      }
	    }
	  }

	  public void close()
	  {
	    if (this.jm260)
	      this.jm262 = false;
	    if (this.jm255 != null)
	      this.jm255.freeConnection(this);
	    else
	      try
	      {
	        XDBManager.getInstance().freeNoPoolConnection(this);
	      }
	      catch (Exception localException)
	      {
	      }
	  }
}
